﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Drawing.Imaging;

namespace FrameWork.FrameWork.Utility
{
    [Serializable]
    public class FileHelper
    {
        //upload file
        public static bool UploadFile(string FullFolder, FileUpload FileUp)
        {
            bool IsSuccess = false;
            try
            {
                if (Directory.Exists(FullFolder) == false)
                    Directory.CreateDirectory(FullFolder);


                //check extension of file
                if (IsFileUp(FileUp.FileName) == true)
                {
                    FileUp.SaveAs(FullFolder + "/" + FileUp.FileName);
                    IsSuccess = true;
                }
            }
            catch
            {
                IsSuccess = false;
            }
            return IsSuccess;

        }

        //Upload and rename file
        public static bool UploadFile(string FullFolder, FileUpload FileUp, string ReName)
        {
            bool IsSuccess = false;
            try
            {
                string FileName = FileUp.FileName;
                if (string.IsNullOrEmpty(FileName))
                    return false;

                if (Directory.Exists(FullFolder) == false)
                    Directory.CreateDirectory(FullFolder);

                //check extension of file
                if (IsFileUp(FileName) == true)
                {
                   // string ext = GetExt(FileName);
                    FileUp.SaveAs(FullFolder + "/" + ReName);
                    IsSuccess = true;
                }
            }
            catch
            {
                return false;
            }
            return IsSuccess;

        }



        //get ext of string file
        public static string GetExt(string strFile)
        {
            string Ext = string.Empty;
            if (!string.IsNullOrEmpty(strFile))
            {
                int indexdot = strFile.LastIndexOf('.');
                if (indexdot < 0)
                    return string.Empty;
                else
                {
                    indexdot = indexdot + 1;
                    Ext = strFile.Substring(indexdot, strFile.Length - indexdot).ToLower();
                }
            }
            return Ext;
        }

        //check file upload
        public static bool IsFileUp(string strFile)
        {
           string ext = GetExt(strFile);

           if (!string.IsNullOrEmpty(ext))
           {
               string Partent = @"(7z|aiff|asf|avi|bmp|csv|doc|fla|flv|gif|gz|gzip|jpeg|jpg|mid|mov|mp3|mp4|mpc|mpeg|mpg|ods|odt|pdf|png|ppt|pxd|qt|ram|rar|rm|rmi|rmvb|rtf|sdc|sitd|swf|sxc|sxw|tar|tgz|tif|tiff|txt|vsd|wav|wma|wmv|xls|xlsx|xml|zip)$";
               return Regex.IsMatch(ext, Partent);
           }
           else
           {
               return false;
           }
           
        }

        //thumbnail
        public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
        {

            Bitmap bmpOut = null;

            try
            {
                Bitmap loBMP = new Bitmap(lcFilename);
                ImageFormat loFormat = loBMP.RawFormat;

                decimal lnRatio;
                int lnNewWidth = 0;
                int lnNewHeight = 0;

                if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
                    return loBMP;

                if (loBMP.Width > loBMP.Height)
                {
                    lnRatio = (decimal)lnWidth / loBMP.Width;
                    lnNewWidth = lnWidth;
                    decimal lnTemp = loBMP.Height * lnRatio;
                    lnNewHeight = (int)lnTemp;
                }
                else
                {
                    lnRatio = (decimal)lnHeight / loBMP.Height;
                    lnNewHeight = lnHeight;
                    decimal lnTemp = loBMP.Width * lnRatio;
                    lnNewWidth = (int)lnTemp;
                }


                bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                Graphics g = Graphics.FromImage(bmpOut);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
                g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);

                loBMP.Dispose();
            }
            catch
            {
                return null;
            }
            return bmpOut;
        }

        //Check file exist
        public static bool CheckExistFile(string lcFileName)
        {
            try
            {
                FileInfo fo = new FileInfo(lcFileName);
                return fo.Exists;
            }
            catch
            {
                return false;
            }
        }

        //Read content from file html
        public static string ReadHtml(string FullPath)
        {

            string Body = string.Empty;
            string html = ReadDoc(FullPath);
            if (!string.IsNullOrEmpty(html))
            {
                string pattern = "<body>((.|\\n)*?)</body>";
                Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase);
                Match match = regEx.Match(html);
                if (match != null && match.Success && match.Groups[1].Success)
                {
                    Body = match.Groups[1].Value;
                }
            }
            return Body;
        }

        //Read content from file doc
        public static string ReadDoc(string FullPath)
        {
            string strContents = string.Empty;
            try
            {
                FileInfo fi = new FileInfo(FullPath);
                if (File.Exists(FullPath))
                {
                    StreamReader sr = File.OpenText(FullPath);
                    strContents = sr.ReadToEnd();
                    sr.Close();
                }
            }
            catch
            {
            }
            return strContents;
        }

        //get byte from image
        public static byte[] getImage(FileUpload fu)
        {
            int len;
            byte[] pic = null;
            if (fu.HasFile)
            {
                len = fu.PostedFile.ContentLength;
                pic = new byte[len];
                fu.PostedFile.InputStream.Read(pic, 0, len);
            }
            return pic;
        }

        //Export Excel
        //Note: Render control before export excel, insert code:
        //public override void VerifyRenderingInServerForm(Control control) { return; } 
        public static void ExportExcel(string FileName, GridView gv, DataTable dt)
        {
            if (string.IsNullOrEmpty(FileName))
                FileName = "Report-" + DateTime.Now.ToString("dd-MM-yyyy");

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + FileName + ".xls");
            HttpContext.Current.Response.ContentType = "application/vnd.xls";
            HttpContext.Current.Response.Charset = "utf-8";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.StringWriter sw = new System.IO.StringWriter(sb);
            HtmlTextWriter htw = new HtmlTextWriter(sw); //grid.AllowPaging = false; //grid.DataBind(); 

            gv.DataSource = dt;
            gv.DataBind();
            gv.RenderControl(htw);

            HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
            HttpContext.Current.Response.Output.Write(sw.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }

        //Download
        public static void DownloadFile(string FullFile)
        {
            if (File.Exists(FullFile))
            {
                string fileName = Path.GetFileName(FullFile);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + "");

                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.WriteFile(FullFile);
            }
        }

        //Download
        public static void DownloadFile(string FullFile, string FileName)
        {
            if (File.Exists(FullFile))
            {               
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + "");

                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.WriteFile(FullFile);
            }
        }

    }
}
